<head>

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<script type="text/javascript" src="wz_jsgraphics.js"></script>

<script>

	[Metadata1] 
	[Metadata2]
	
	var URI="[URL]";
	var show=[wheel/tree];
	var gXmlDoc;

	 function convertSpaces(s){	 
		var index=s.indexOf(" && ");
		while (index!=-1){
			s = s.substring(0,index)+"^"+s.substring(index+4);
			index=s.indexOf(" && ");
		}	 
		index=s.indexOf("\\\"");
		while (index!=-1){
			s = s.substring(0,index)+"~"+s.substring(index+2);
			index=s.indexOf("\\\"");
		}
		for(var i=0; i<s.length; i++){
			if(s.charAt(i) == ' ')
				s = s.substring(0,i)+"+"+s.substring(i+1);
		}
		return s;
	}

	function convertPlus(s){
		for(var i=0; i<s.length; i++){
			if(s.charAt(i) == '+')
				s = s.substring(0,i)+" "+s.substring(i+1);
			if(s.charAt(i) == '^')
				s = s.substring(0,i)+" && "+s.substring(i+1);			
			if(s.charAt(i) == '~')
				s = s.substring(0,i)+"\\\\\\\""+s.substring(i+1);
		}
		return s;
	}

	function search(){
		var query=document.getElementById("text").value;
		if (query!="")
			window.location = URI+"/search?query="+convertSpaces(query);	
	}
	
	function searchInResult(){
		var query=document.getElementById("text").value;
		if (query!=""){
			if ([TopicId]==-1)
				window.location = URI+"/search?query="+convertSpaces("[Query]^"+query);
			else
				window.location = URI+"/search?query="+convertSpaces("[Query]^"+query)+"&topic=[TopicId]";
			
		}	
	}

	function showFirstAndPrevPages(topic){
		var ans="";
		if ([Page]>0){
			ans+=
			"<a href=\""+URI+
			"/search?query="+convertSpaces("[Query]")+
			"&page=0"+topic+"\">";
		}
		ans+="First ";
		if ([Page]>0){
			ans+="</a> &nbsp";
			ans+="<a href=\""+URI
			+"/search?query="+convertSpaces("[Query]")
			+"&page="+([Page]-1)+topic+"\">";
		}		
		ans+="Previous ";
		if ([Page]>0){
			ans+="</a> ";
		}
		return ans;
	}
	
	function showLastAndNextPages(topic,countPages){
		var ans="";	
		if ([Page]<countPages-1){
			ans+=
			"<a href=\""+URI+
			"/search?query="+convertSpaces("[Query]")+
			"&page="+([Page]+1)+topic+
			"\">";
		}
	
		ans+="Next ";
		if ([Page]<countPages-1){
			ans+="</a> &nbsp";
			ans+="<a href=\""+URI+
			"/search?query="+convertSpaces("[Query]")+
			"&page="+(countPages-1)+topic+"\">";
		}
		ans+="Last ";
		if ([Page]<countPages-1){
			ans+="</a> ";
		}
		return ans;
	}
	
	function showPageNumber(num,topic){
		var ans="";	
		if (num!=[Page]){
			ans+="<a href=\""+URI+
			"/search?query="+convertSpaces("[Query]")+
			"&page="+num+topic+"\">";
		}
		ans+=" "+(num+1)+" ";
		if (num!=[Page] ){
			ans+="</a> ";
		}	
		return ans;
	}

	function showClosePages(topic,countPages){
		var start=([Page]>5)?[Page]-5:0;
		var end=([Page]+5<=countPages-1)?[Page]+5:countPages-1;
		var ans="";
		for(var i=start;i<=end;i++){
			ans+="&nbsp"+showPageNumber(i,topic);
		}
		if ([Page]!=countPages-1)
			ans+="&nbsp";
		return ans;
	}

	function showNeighborPages(coutnArticles){
		var ans="";
		var topic=([TopicId]==-1)?"":"&topic=[TopicId]";
		
		var countPages=(coutnArticles - (coutnArticles % [CountInPage])) / [CountInPage];
		if ((coutnArticles % [CountInPage])!=0)
			countPages++;
		
		ans+=" Found "+coutnArticles+" articles <br />";
		ans+=" Page "+([Page]+1)+" from "+countPages;
		ans+=" pages between "+([CountInPage]*[Page]+1)+" "+" to "+
		(([Page]<countPages-1)?([CountInPage]*([Page]+1)):coutnArticles)+"<br /><br />";
		
		if (countPages!=1){
			ans+="<b>Pages: </b>";
			ans+=showFirstAndPrevPages(topic);
			ans+=showClosePages(topic,countPages);
			ans+=showLastAndNextPages(topic,countPages);
		}

		return ans;
	}

	function showTitle(articlesArray){
		return "<b><a href=\""+URI+"/article?id="+
			articlesArray.getElementsByTagName("id")[0].childNodes[0].nodeValue+"\">"+	
			articlesArray.getElementsByTagName("title")[0].childNodes[0].nodeValue+
			"</a><br /></b>";
	} 
	
	function showMetadata(articlesArray){
		var ans="";
		for(var i=0; i<metadata.length;i++){
			ans+=metadata2[i]+": "+articlesArray.getElementsByTagName(metadata[i])[0].childNodes[0].nodeValue+" ";			
			if (i!=metadata.length-1)
				ans+="<br />";
		}
		return ans+"<br />";
	}

	function showTopics(articlesArray){
		var ans="Topics: ";		
		var allTopicsId=articlesArray.getElementsByTagName("topicsID")[0].childNodes[0].nodeValue;	
		
		if (allTopicsId.length==0)
			return "No topics <br />";		
		allTopicsId=allTopicsId.split(",");
		var allTopicsName=articlesArray.getElementsByTagName("topicsName")[0].childNodes[0].nodeValue;
		
		//allTopicsName=allTopicsName.split(",");
		
		allTopicsName=allTopicsName.split("],[");
		
		allTopicsName[0]+="]";
		allTopicsName[allTopicsName.length-1]="["+allTopicsName[allTopicsName.length-1];
		
		for(var i=1;i<allTopicsName.length-1;i++)
			allTopicsName[i]="["+allTopicsName[i]+"]";
			//allTopicsName[i]=allTopicsName[i];
		
		for(var i=0 ; i<allTopicsId.length ; i++){
			ans+="<a href=\""+URI+"/search?query="
			+convertSpaces("[Query]")+"&page=0&topic="+allTopicsId[i]+"\">"+
				allTopicsName[i]+"</a>, ";
		}
		return ans+"<br />";
	}

	function removeFromQuery(index,splited){
	
		var first=true;
	
		var topics="[TopicId]".split(",");
		if ([TopicId]==-1){
			var ans="";
			for(var i=0;i<splited.length;i++){
				if (i!=index){
					if (first){
						ans=splited[i];
						first=false;
					}
					else{
						ans+="^"+splited[i];
					}
				}
			}
			return URI+"/search?query="+ans;
		}
		else{
			if (index>=splited.length-topics.length){
				var url2=URI+"/search?query="+convertSpaces("[Query]");				
				
				for(var i=splited.length-topics.length;i<splited.length;i++){
					if (i!=index){
						if (first){
							first=false;
							url2+="&topic="+splited[i].split(":")[1];
						}
						else
							url2+=","+splited[i].split(":")[1];
					}//end if
				}//end for
				return url2;
			}
			else{
				var ans="";
				for(var i=0;i<splited.length-topics.length;i++){
					if (i!=index){
						if (first){
							ans=splited[i];
							first=false;
						}
						else{
							ans+="^"+splited[i];
						}
					}
				}
				return URI+"/search?query="+ans+"&topic=[TopicId]";
			}
		}
	}
	
	function removeOthersFromQuery(index,splited){
		if ([TopicId]==-1){
			return URI+"/search?query="+splited[index];
		}
		else{
			var topics="[TopicId]".split(",");
			if (index>=splited.length-topics.length)
				return URI+"/search?query=*:*&topic="+splited[index].split(":")[1];
			else
				return URI+"/search?query="+splited[index];
		}
	}

	function showMainTitle(xmlDoc){
		var title=document.getElementById("resultTitle");
		var array1 = xmlDoc.getElementsByTagName("this");
		
		title.innerHTML+="Search results for ";
		
		var splited;
		var names;
		
		if ([TopicId]!=-1){
			var allTopics="[TopicId]".split(",");
			var str="Topic:"+allTopics[0];
			var strName = "Topic:"+array1[0].getElementsByTagName("name")[0].childNodes[0].nodeValue; //The name of the topic
			for(var i=1;i<allTopics.length;i++){
				str+=" && Topic:"+allTopics[i];
				strName+=" && Topic:"+array1[i].getElementsByTagName("name")[0].childNodes[0].nodeValue; //The name of the topic
			}
			if ("[Query]"!="*:*"){
				splited=convertSpaces("[Query] && "+str).split("^");
				names=convertSpaces("[Query] && "+strName).split("^");
			}
			else{
				splited=convertSpaces(str).split("^");
				names=convertSpaces(strName).split("^");
			}
		}
		else{
			splited=convertSpaces("[Query]").split("^");
			names=convertSpaces("[Query]").split("^");
		}
		
		if (splited.length==1){
			if ([TopicId]==-1)
				title.innerHTML+="query \"[Query]\"";
			else
				title.innerHTML+="topic \""+array1[0].getElementsByTagName("name")[0].childNodes[0].nodeValue+"\"";
		}
		else{
			var i=0;
			for(;i<splited.length;i++){
				title.innerHTML+="<a href=\""+removeFromQuery(i,splited)+"\">[x]</a> ";
				title.innerHTML+="<a href=\""+removeOthersFromQuery(i,splited)+"\">"+convertPlus(names[i])+"</a> ";
				if (i!=splited.length-1)
				title.innerHTML+="and ";
			}
		}
	}

	function showArticles(xmlDoc){		
		var articlesArray = xmlDoc.getElementsByTagName("article");		
		var articlesList=document.getElementById("articlesList");
		
		for (var i = 0; i < articlesArray.length; i++) {
			articlesList.innerHTML+=
				showTitle(articlesArray[i]) + showMetadata(articlesArray[i]);
				
			if (articlesArray[i].getElementsByTagName("summary")[0].childNodes.length==0)
				articlesList.innerHTML+=
					"No summary <br />";
			else
				articlesList.innerHTML+=
					articlesArray[i].getElementsByTagName("summary")[0].childNodes[0].nodeValue +"<br />";
			articlesList.innerHTML+=
				showTopics(articlesArray[i]) 
				+ "<br /> <br />";
		}
		
		var generalArray = xmlDoc.getElementsByTagName("general");
		if (articlesArray.length!=0)
			articlesList.innerHTML+=showNeighborPages(generalArray[0].getElementsByTagName("countArticles")[0].childNodes[0].nodeValue);
		else
			articlesList.innerHTML+="Sorry, we haven't found any results <br /> Try other text";
	}
	
	function contains(arr,element){
		for(var i=0;i<arr.length;i++){
			if (arr[i]==element)
				return true;
		}
		return false;
	}
	
	function showNoWheel(xmlDoc){
		var hierarchical=document.getElementById("hierarchicalTopics");		
		hierarchical.innerHTML="";
	
		if ([TopicId]==-1){
			showTopicsList(xmlDoc,"topic","Hierarchical Topics Tree",true);
			if (contains(metadata,"cat"))
				showCatsList(xmlDoc,"relateCat","Related Categories",-1);
		}
		else
			showAllTopics2(xmlDoc);	
	}

	function showResult(xmlDoc){
		
		showMainTitle(xmlDoc);
		showArticles(xmlDoc);
		
		whatToShow(show);
	}
	
	function isContainInArray(arr,x){
		for(var i=0;i<arr.length;i++)
			if (arr[i]==x)
				return true;
		return false;
	}

	function showoneTopic(topicArray,haveLevel,thisTopicId){
		if (thisTopicId!=null)
			if (isContainInArray(thisTopicId,topicArray.getElementsByTagName("id")[0].childNodes[0].nodeValue)){
				return ""; 						
			}
		var ans="";
		if (haveLevel){
			var level=topicArray.getElementsByTagName("level")[0].childNodes[0].nodeValue;		
			for(var i=0;i<level;i++){
				ans+="&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
			}
		}
		
		var allTopic=new Array();
		if (thisTopicId!=null)
			for(var i=0;i<thisTopicId.length;i++)
				allTopic[i]=thisTopicId[i];
		allTopic[allTopic.length]=topicArray.getElementsByTagName("id")[0].childNodes[0].nodeValue;
				
		ans+="<a href=\""+URI+"/search?query="+
		convertSpaces("[Query]")+"&page=0&topic="+allTopic+"\">"+
				topicArray.getElementsByTagName("name")[0].childNodes[0].nodeValue+" ("+
				topicArray.getElementsByTagName("countFound")[0].childNodes[0].nodeValue+")</a> <br />";		
		return ans;
	}

	function showoneCat(topicArray,thisTopicId){
		
		var name=topicArray.getElementsByTagName("name")[0].childNodes[0].nodeValue;
		
		var ans="<a href=\""+URI+"/search?query="+
		convertSpaces("[Query]")+"^cat:~"+name+"~&page=0&topic="+thisTopicId+"\">"+name+" ("+
				topicArray.getElementsByTagName("countFound")[0].childNodes[0].nodeValue+")</a> <br />";		
		return ans;
	}

	function showAllTopics2(xmlDoc){
		var thisArray = xmlDoc.getElementsByTagName("this");
		
		var thisIdArr=new Array(); 
		
		for(var i=0;i<thisArray.length;i++){
			thisIdArr[i]=thisArray[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
		}
	
		var hierarchical=document.getElementById("hierarchicalTopics");

		
		showTopicsList(xmlDoc,"relateTopic","Related Topics",false,thisIdArr);	
		
		showCatsList(xmlDoc,"relateCat","Related Categories",thisIdArr);
	}

	function topicsArraySort(topicsArray){
		for(var i=0;i<topicsArray.length;i++)
			for(var j=i+1;j<topicsArray.length;j++){
				var i2=(topicsArray[i].getElementsByTagName("countFound")[0].childNodes[0].nodeValue);
				var j2=(topicsArray[j].getElementsByTagName("countFound")[0].childNodes[0].nodeValue);
				if (parseInt(i2)<parseInt(j2)){					
					var temp=topicsArray[i];					
					topicsArray[i]=topicsArray[j];					
					topicsArray[j]=temp;
				}
			}
	}
	
	function copyTopicsArray(topicsArry){
		var ans=new Array();
		for(var i=0;i<topicsArry.length;i++){
			ans[i]=topicsArry[i];
		}
		return ans;
	}

	function showTopicsList(xmlDoc,tagName,text,haveLevel,thisId){
		var hierarchical=document.getElementById("hierarchicalTopics");
		topicssArray = xmlDoc.getElementsByTagName(tagName);
		topicssArray=copyTopicsArray(topicssArray);
		if (topicssArray.length>0){
			topicsArraySort(topicssArray);
			hierarchical.innerHTML+="<br />"+text+"<br />";
			for (var i = 0; i < topicssArray.length & i<[NumberOfTopicsToShow]; i++){ 		
				hierarchical.innerHTML+=showoneTopic(topicssArray[i],haveLevel,thisId);
			}
		}
	}

	function showCatsList(xmlDoc,tagName,text,thisId){
	
		var generalArray = xmlDoc.getElementsByTagName("general");
		var countAll=generalArray[0].getElementsByTagName("countArticles")[0].childNodes[0].nodeValue;
	
	
		var hierarchical=document.getElementById("hierarchicalTopics");		
		catsArray = xmlDoc.getElementsByTagName(tagName);		
		catsArray=copyTopicsArray(catsArray);
		
		if (catsArray.length>0){
			topicsArraySort(catsArray);
			hierarchical.innerHTML+="<br />"+text+"<br />";
			for (var i = 0; i < catsArray.length & i<[NumberOfTopicsToShow]; i++){
				var count=catsArray[i].getElementsByTagName("countFound")[0].childNodes[0].nodeValue;
				if (countAll<5*count && 
				("[Query]".indexOf("cat:\\\""+catsArray[i].getElementsByTagName("name")[0].childNodes[0].nodeValue+"\\\"")<0)){
					hierarchical.innerHTML+=showoneCat(catsArray[i],thisId);
				}
			}
		}
	}


	function showWheel(xmlDoc){
		var hierarchical=document.getElementById("hierarchicalTopics");		
		hierarchical.innerHTML="";
		
		var centerX=150;
		var centerY=180;
		var length2=95;
		var length3=140;
		var coter=60;
		var boxX=140;
				
		var jg_doc = new jsGraphics("hierarchicalTopics"); // draw directly into document
		jg_doc.setColor("#ddffee");
		jg_doc.fillEllipse(centerX, centerY, coter, coter);
		
		var thisArray = xmlDoc.getElementsByTagName(([TopicId]==-1)?"topic":"relateTopic");		
		var thisIdArr=new Array(); 
		
		var tempArry=copyTopicsArray(thisArray);
		thisArray=new Array();
		for(var i=0;i<tempArry.length;i++){
			if (!contains("[TopicId]".split(","),tempArry[i].getElementsByTagName("id")[0].childNodes[0].nodeValue))
				thisArray[thisArray.length]=tempArry[i];
		}		
		
		topicsArraySort(thisArray);
		var min=Math.min(thisArray.length,[NumberInWheel]);					
		
		var minLength=0;
		if (min>0){
			minLength=thisArray[min-1].getElementsByTagName("countFound")[0].childNodes[0].nodeValue;
		}
		
		for(var i=0;i<min;i++){
			var myLength=thisArray[i].getElementsByTagName("countFound")[0].childNodes[0].nodeValue
			var x2=centerX+(coter/2+(minLength/myLength*(length2-coter/2)))*Math.cos(-Math.PI/2+i*2*Math.PI/min)+coter/2;
			var y2=centerY+(coter/2+(minLength/myLength*(length2-coter/2)))*Math.sin(-Math.PI/2+i*2*Math.PI/min)+coter/2;
			var x3=centerX+length3*Math.cos(-Math.PI/2+i*2*Math.PI/min)+coter/2;
			var y3=centerY+length3*Math.sin(-Math.PI/2+i*2*Math.PI/min)+coter/2;
			
			var allTopic;
			
			if ([TopicId]==-1){
				allTopic=thisArray[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
			}
			else{
				var topic=[[TopicId]]+"";
				if (topic.charAt(0)=='['){
					allTopic=thisArray[i].getElementsByTagName("id")[0].childNodes[0].nodeValue+
					","+topic.substring(1,topic.length-1);
				}
				else{
					allTopic="[TopicId],"+thisArray[i].getElementsByTagName("id")[0].childNodes[0].nodeValue+"";
				}
			}
			
			var link="/search?query="+convertSpaces("[Query]")+"&page=0&topic="+allTopic+"&wheel=yes";
		
			jg_doc.setStroke(10);
			jg_doc.drawLine(centerX+coter/2, centerY+coter/2,x2,y2);
			jg_doc.drawStringRect("<a href=\""+link+"\">"+thisArray[i].getElementsByTagName("name")[0].childNodes[0].nodeValue+"("+myLength+")</a>", x3-boxX/2, y3-15,boxX,"center"); 
		}
		jg_doc.paint(); // draws, in this case, directly into the document
	}

	onload = function(){ 	
		var xmlHttp = new XMLHttpRequest();
		
		var url2; 
		
		if ([TopicId]==-1){
			url2=URI+"/result?query="
			+convertSpaces("[Query]")+
			"&page=[Page]&topic=[TopicId]";
		}
		else{			
			url2=URI+"/result?query="
			+convertSpaces("[Query]");
			var splited = "[TopicId]".split(",");
			for(var i=0;i<splited.length;i++){
				url2+="^content_type:"+splited[i];
			}
			url2+="&page=[Page]&topic=[TopicId]";
		}
		
		xmlHttp.open("GET", url2, true);
		xmlHttp.setRequestHeader("Content-Type", "text/xml");
		
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {		
				//alert(xmlHttp.responseText);
				gXmlDoc=xmlHttp.responseXML;
				showResult(xmlHttp.responseXML)
				
			}
		};
		//xmlHttp.overrideMimeType("text/html; charset=UTF-8");
		xmlHttp.send();
	};
	
	function whatToShow(what){
		show=what;
		if(show==1)
			showNoWheel(gXmlDoc);
		else
			showWheel(gXmlDoc);
	}
	
</script>


	
<title>Learning Content Models for Semantic Search Project</title>

<style type="text/css">
h01 {
	display: block;
	background-color: #B9C9FE;
	letter-spacing: 0.3em;
	color: #0033BC;
	text-align: center;
}

h02 {
	display: block;
	background-color: #E8EDFF;
	letter-spacing: 0.3em;
	color: #666699;
	text-align: center;
}

</style>
</head>

<body>

<h01>Learning Content Models for Semantic Search</h01>
<hr />
<h02>Results</h02>

<div id="resultTitle" style="position: absolute; top: 80px; left: 20px; width:1230px;" align="center">
</div>

<div id="buttonsAndTextbox" style="position: absolute; top: 90px; left: 20px;" align="center">

<br /><br /><br />
<input type="text" id="text" size="270"/>
<br /><br />
<input id="btnSearch" type="button" value="New Search" onclick="search()" />
<input id="btnSearchInResults" type="button" value="Search In Results" onclick="searchInResult()" />
</div>

<div id="topics" style="position: absolute; top: 220px; left: 20px; width:350px;" >
<input id="btnWheel" type="button" value="Wheel Topics" onclick="whatToShow(2)" />
<input id="btnTree" type="button" value="Tree Topics" onclick="whatToShow(1)" />
<br />
</div>

<div id="hierarchicalTopics" style="position: absolute; top: 240px; left: 30px; width:370px;" >
<br />
</div>

<div id="articlesList" style="position: absolute; top: 220px; left: 430px;">

</div>

</body>
</html>
